def get_module_name(app,view_func_name):
    # 从 view_functions 字典中获取视图函数对象
    view_func = app.view_functions.get(view_func_name)

    if view_func:
        # 获取视图函数所在的模块名称
        module_name = view_func.__module__
        return module_name
    else:
        return None

with app.test_request_context():
    # 获取所有规则
    rules = list(app.url_map.iter_rules())
    for rule in rules:
        # 打印每个规则及其对应的视图函数名
        print(f"Rule: {rule}, View Function Name: {rule.endpoint}")
        print(get_module_name(app, rule.endpoint))


------ 反射根据视图名称获取模块名称

import inspect
from flask import Flask, request

app = Flask(__name__)


@app.route('/hello')
def hello_world():
    # 获取当前视图函数名称
    endpoint = request.endpoint

    # 获取 Flask 应用的 view_functions 字典
    view_functions_dict = inspect.getattr_static(app, 'view_functions')

    if endpoint:
        # 从 view_functions 字典中获取视图函数对象
        view_func = view_functions_dict.get(endpoint)

        if view_func:
            # 从视图函数对象获取模块名称
            module_name = view_func.__module__
            return f"The current endpoint is: {endpoint} and the module name is: {module_name}"
        else:
            return "Endpoint not found", 404
    else:
        return "Endpoint not available", 404


if __name__ == '__main__':
    app.run()